<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="30%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-form-item label="品牌名称" prop="brandName" required>
          <el-input v-model="form.brandName" placeholder="请输入" maxlength="50"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addBrand, updateBrand} from "../../../api/imsMng";

export default {
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      form: {},
      titleList: ['新增', '详情', '修改']
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          if (this.flag == 2) {
            updateBrand(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            })
          } else {
            addBrand(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            })
          }
        }
      })
    },
  }
}
</script>
